package com.yanqu.road.server.manager.activity.hanghai;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossHangHaiMgr extends TempMgr {

    // 预留参数
    public static Map<Integer, String> hotUpdateMap = new ConcurrentHashMap<>();

    private static Map<Integer, CrossHangHaiActivityGroup> activityMap = new ConcurrentHashMap<>();

    public static synchronized CrossHangHaiActivity getActivity(int activityId, int groupId) {
        // 拿分组配置
        UnionActivityGroup groupData = Cross2UnionActivityGroupMgr.getGroupData(activityId, groupId);
        if (groupData == null) {
            return null;
        }
        // 拿活动
        if (activityMap.get(activityId) == null) {
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            if (activityInfo == null) {
                return null;
            }
            CrossHangHaiActivityGroup activity = new CrossHangHaiActivityGroup(activityInfo);
            activityMap.put(activityId, activity);
        }
        // 拿分组数据
        CrossHangHaiActivityGroup group = activityMap.get(activityId);
        return group.getActivity(groupId);
    }

    public static Map<Integer, CrossHangHaiActivityGroup> getActivityMap() {
        return activityMap;
    }

    @Override
    public boolean reloadData() throws Exception {
        // 获取活动
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.HangHaiActivity.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        // 重新加载数据
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (!activityMap.containsKey(activityInfo.getActivityId())) {
                Map<Integer, UnionActivityGroup> groupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
                if (groupMap != null) {
                    for (int groupId : groupMap.keySet()) {
                        getActivity(activityInfo.getActivityId(), groupId);
                    }
                }
            }
        }
        return true;
    }

    @Override
    public boolean save() {
        // 活动数据保存
        for (CrossHangHaiActivityGroup group : activityMap.values()) {
            group.save();
        }

        // 移除过期12小时的活动
        long now = System.currentTimeMillis() / 1000;
        for (Map.Entry<Integer, CrossHangHaiActivityGroup> entry : activityMap.entrySet()) {
            ActivityInfo activityInfo = entry.getValue().getActivityInfo();
            if (now > activityInfo.getEndShowTime() + DateHelper.HOUR_SECONDS * 12) {
                activityMap.remove(entry.getKey());
                getLogger().error("cross hang hai activity {} expire, remove data.", entry.getKey());
            }
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
